package net.kindroid.fm;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

public class CalibrateView extends View
{
	/**
	 * long line
	 */
	public float longer;

	/**
	 * short line
	 */
	private float shorter;

	/**
	 * long line x
	 */
	private float long_X;

	/**
	 * long line start y
	 */
	private float long_start_Y;

	/**
	 * long line end y
	 */
	private float long_end_Y;

	/**
	 * short line x
	 */
	private float short_X;

	/**
	 * short line start y
	 */
	private float short_start_Y;

	/**
	 * short line end y
	 */
	private float short_end_Y;

	/**
	 * red line x
	 */
	public float red_line_X;

	/**
	 * red line start y
	 */
	private float red_line_start_Y;

	/**
	 * red line
	 */
	private float red_line_end_Y;

	/**
	 * screen width
	 */
	public int screen_width;

	/**
	 * screen width
	 */
	public int screen_height;
	
	/**
	 * total vertical
	 */
	private int totalVertical;

	/**
	 * start long line
	 */
	private float startLongLine;

	/**
	 * space
	 */
	public double space;

	/**
	 * red line height
	 */
	public float red_line_height;

	/**
	 * red line top margin
	 */
	public float red_line_margin;

	/**
	 * left margin
	 */
	public float left_margin;

	/**
	 * right margin
	 */
	public float right_margin;
	
	public float red_line_top_matrix;
	
	public float red_line_height_matrix;
	
	public int small_digit_text_size;
	
	public int margin;

	/**
	 * paint
	 */
	private Paint mPaint;

	/**
	 * small digit paint
	 */
	private TextPaint mTextPaint;

	/**
	 * black background
	 */
	private Bitmap bitmap;

	/**
	 * background height
	 */
	private float height;

	/**
	 * small digit x
	 */
	private float digit_X;

	/**
	 * small digit margin top
	 */
	public int small_digit_margin_top;

	/**
	 * small digit scale
	 */
	private float small_digit_scale;
	
	private int pointer_height;
	
	/**
	 * init flag
	 */
	private boolean mIsInitDone = false;

	public CalibrateView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initSize();
	}

	/**
	 * init data
	 */
	private void initSize()
	{
		pointer_height = this.getResources().getDrawable(R.drawable.pointer_bg).getMinimumHeight();
		longer = red_line_height - red_line_margin * 2;
		shorter = red_line_height / 2f;
		left_margin = 18f;
		right_margin = 17f;
		totalVertical = 42;
		startLongLine = 5;
		small_digit_scale = 1.3f * screen_height / 480f;

		height = red_line_height;

		long_X = left_margin;
		long_start_Y = red_line_margin;
		long_end_Y = red_line_height - long_start_Y;

		short_X = left_margin;
		short_start_Y = (red_line_height - shorter) / 2;
		short_end_Y = red_line_height - short_start_Y;

		red_line_start_Y = 0;
		red_line_end_Y = red_line_height;
	}

	public void onDraw(Canvas canvas)
	{
		if(!mIsInitDone)
		{
			initSize();
			mIsInitDone = true;
		}
		drawBackground(canvas);
		drawLine(canvas);
		drawRedLine(canvas);
		drawSmallDigit(canvas);
		super.onDraw(canvas);
	}

	/**
	 * draw background
	 * 
	 * @param canvas
	 */
	private void drawBackground(Canvas canvas)
	{
		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pointer_bg);
		canvas.drawBitmap(bitmap, getBackgroundMatrix(), null);

	}

	/**
	 * draw line
	 * 
	 * @param canvas
	 */
	private void drawLine(Canvas canvas)
	{
		mPaint = new Paint();
		if (space != 0)
		{
			for (int i = 0; i < totalVertical; i++)
			{
				if (i < 2 || i > 37)
				{
					mPaint.reset();
					mPaint.setColor(this.getResources().getColor(R.color.dark_line_color));
				} else if (i < 4 || i > 35)
				{
					mPaint.reset();
					mPaint.setColor(this.getResources().getColor(R.color.dark_to_light_one));
				} else if (i < 6 || i > 33)
				{
					mPaint.reset();
					mPaint.setColor(this.getResources().getColor(R.color.dark_to_light_two));
				} else if (i < 8 || i > 31)
				{
					mPaint.reset();
					mPaint.setColor(this.getResources().getColor(R.color.dark_to_light_three));
				} else if (i < 10 || i > 29)
				{
					mPaint.reset();
					mPaint.setColor(this.getResources().getColor(R.color.dark_to_light_four));
				} else if (i < 12 || i > 27)
				{
					mPaint.reset();
					mPaint.setColor(this.getResources().getColor(R.color.dark_to_light_five));
				} else if (i < 14 || i > 25)
				{
					mPaint.reset();
					mPaint.setColor(this.getResources().getColor(R.color.dark_to_light_six));
				} else
				{
					mPaint.reset();
					mPaint.setColor(this.getResources().getColor(R.color.light_line_color));
				}
				float floatX = (float) (long_X + space * i);
				if ((i - startLongLine) % 10 == 0)
				{
					canvas.drawLine(floatX, long_start_Y, floatX, long_end_Y, mPaint);
				} else
				{
					canvas.drawLine(floatX, short_start_Y, floatX, short_end_Y, mPaint);
				}
			}
		}
	}

	/**
	 * draw red line
	 * 
	 * @param canvas
	 */
	private void drawRedLine(Canvas canvas)
	{
		canvas.drawBitmap(((BitmapDrawable) this.getResources().getDrawable(R.drawable.pointer)).getBitmap(), getMatrix(), null);
	}

	/**
	 * draw small digit
	 * 
	 * @param canvas
	 */
	private void drawSmallDigit(Canvas canvas)
	{
		mTextPaint = new TextPaint();
		mTextPaint.setTextSize(small_digit_text_size);
		mTextPaint.setColor(this.getResources().getColor(R.color.digit_color));
		// draw 90
		digit_X = (float) (left_margin + startLongLine * space - margin);
		canvas.drawText("9", digit_X, height + small_digit_margin_top, mTextPaint);
		digit_X += margin;
		canvas.drawText("0", digit_X, height + small_digit_margin_top, mTextPaint);

		// draw 95
		digit_X = (float) (left_margin + (startLongLine + 10) * space - margin);
		canvas.drawText("9", digit_X, height + small_digit_margin_top, mTextPaint);
		digit_X += margin;
		canvas.drawText("5", digit_X, height + small_digit_margin_top, mTextPaint);

		// draw 100
		digit_X = (float) (left_margin + (startLongLine + 20) * space - margin * 1.5f);
		canvas.drawText("1", digit_X, height + small_digit_margin_top, mTextPaint);
		digit_X += margin;
		canvas.drawText("0", digit_X, height + small_digit_margin_top, mTextPaint);
		digit_X += margin;
		canvas.drawText("0", digit_X, height + small_digit_margin_top, mTextPaint);

		// draw 105
		digit_X = (float) (left_margin + (startLongLine + 30) * space - margin * 1.5f);
		canvas.drawText("1", digit_X, height + small_digit_margin_top, mTextPaint);
		digit_X += margin;
		canvas.drawText("0", digit_X, height + small_digit_margin_top, mTextPaint);
		digit_X += margin;
		canvas.drawText("5", digit_X, height + small_digit_margin_top, mTextPaint);
		System.out.println("height + small_digit_margin_top = " + (height + small_digit_margin_top));
	}

	/**
	 * red line matrix
	 * 
	 * @return matrix
	 */
	private Matrix getMatrix()
	{
		Matrix matrix = new Matrix();
		float[] mf = new float[9];
		matrix.getValues(mf);
		mf[0] = 1.0f;
		mf[2] = red_line_X;
		mf[4] = red_line_height_matrix; //height
		mf[5] = red_line_top_matrix;
		matrix.setValues(mf);
		return matrix;
	}

	/**
	 * background matrix
	 * 
	 * @return matrix
	 */
	private Matrix getBackgroundMatrix()
	{
		Matrix matrix = new Matrix();
		float[] mf = new float[9];
		matrix.getValues(mf);
		mf[0] = screen_width / 240f;
		mf[4] = red_line_height / pointer_height;
		matrix.setValues(mf);
		return matrix;
	}
}
